<template>
  <el-dialog v-model="dialogVisible" :destroy-on-close="true" width="640px" :title="`${dialogProps.title}轮播图`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="dialogProps.isView"
      :model="dialogProps.row"
      :hide-required-asterisk="dialogProps.isView"
    >
      <el-form-item label="轮播图" prop="imageUrl">
        <UploadImg v-model:image-url="dialogProps.row!.imageUrl" width="300px" height="180px" :file-size="3">
          <template #empty>
            <el-icon><Picture /></el-icon>
          </template>
          <template #tip> 点击上传图片 </template>
        </UploadImg>
      </el-form-item>
      <el-form-item label="轮播图标题" prop="name">
        <el-input v-model="dialogProps.row!.name" placeholder="请输入标题" clearable></el-input>
      </el-form-item>
      <el-form-item label="跳转链接" prop="link">
        <el-input v-model="dialogProps.row!.link" placeholder="请输入跳转链接" clearable></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input v-model="dialogProps.row!.sort" placeholder="请输入" clearable></el-input>
      </el-form-item>
      <el-form-item label="是否启用" prop="status">
        <el-select class="w100" v-model="dialogProps.row!.status">
          <el-option v-for="item in advertStatus" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="dialogVisible = false"> 取消 </el-button>
      <el-button v-show="!dialogProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="MemberDialog">
import { ref, reactive } from "vue";
import { advertStatus } from "@/utils/serviceDict";
import { ElMessage, FormInstance } from "element-plus";
import { Member } from "@/api/interface";
import UploadImg from "@/components/Upload/Img.vue";

const rules = reactive({
  imageUrl: [{ required: true, message: "请上传图片" }]
});

interface DialogProps {
  title: string;
  isView: boolean;
  row: Partial<Member.ResMemberList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const dialogVisible = ref(false);
const dialogProps = ref<DialogProps>({
  isView: false,
  title: "",
  row: {}
});
// 接收父组件传过来的参数
const acceptParams = async (params: DialogProps) => {
  dialogProps.value = params;
  if (dialogProps.value.title === "新增") {
    dialogProps.value.row.status = true;
  }
  dialogVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await dialogProps.value.api!(dialogProps.value.row);
      ElMessage.success({ message: `${dialogProps.value.title}轮播图成功！` });
      dialogProps.value.getTableList!();
      dialogVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
